package com.mainworks.commons.crypto;

import com.mainworks.commons.constant.Constants;
import com.mainworks.commons.utils.StringCustomUtils;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * @Author: HuangMinJie
 * @CreateDate: 13:08 2021/10/28
 * @Description:
 * @Version: 1.0.0.0
 * @ModifiedBy:
 * @ModifiedDate:
 * @ModifiedDescription:
 */

public class CryptoJs {
    public static void main(String [] args){
        //String str= "MNm02/sa3c2ru5XBZp+J7DpcRiE7x4IvtaF8ma2EerZYHODg+r19dHpM9+b9YQEO1JNKnz/3x4wu+uQBkwhS5Z0p6RP6OdWYXm4Pj8LlaWsbDbBph63R7ULgkE3wJjv4xBB/TOgnW98NOF8xLcI8J2gKYYNuSPUvbZxZU9RM3j9DyrhYSlLktG+7vuGaW5p5jwkN12WcwCWWVhGIhOmVSuU0QRRyjEsiNS5vUPqeVSbSwftVuMY5uz4f/msDK5m7agJZbXStANXocaRp5B6VgBx/PQ9TBDIlkn8VQYGtwQHWc6WYIe7rXAP+Gs+C1jNFylJX4fWdlxSF/k3Vq0LRvuoptce1U+VxwYzdY3NCm7F/3xAYdEyIYXcCUZPgLATm+1oExRCXKXIMakIVnVvY4w==";
        // String str= "MNm02/sa3c2ru5XBZp+J7DpcRiE7x4IvtaF8ma2EerZYHODg+r19dHpM9+b9YQEO1JNKnz/3x4wu+uQBkwhS5Z0p6RP6OdWYXm4Pj8LlaWsbDbBph63R7ULgkE3wJjv4xBB/TOgnW98NOF8xLcI8J2gKYYNuSPUvbZxZU9RM3j9DyrhYSlLktG+7vuGaW5p5jwkN12WcwCWWVhGIhOmVSuU0QRRyjEsiNS5vUPqeVSbSwftVuMY5uz4f/msDK5m7agJZbXStANXocaRp5B6VgBx/PQ9TBDIlkn8VQYGtwQHWc6WYIe7rXAP+Gs+C1jNFylJX4fWdlxSF/k3Vq0LRvuoptce1U+VxwYzdY3NCm7F/3xAYdEyIYXcCUZPgLATm+1oExRCXKXIMakIVnVvY4w==";

        // System.out.println(decrypt(str));
    }

    private static String key = Constants.CryptoJsKEY;//;

    /**
     * 加密
     * @param content 明文
     * @return 密文
     */
    public static String encrypt(String content){
        String encrypt = null;
        if (StringCustomUtils.isNotEmpty(content) == false) {
            return null;
        }
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES"));
            byte[] encryptStr = encryptStr = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
            encrypt = Base64.getEncoder().encodeToString(encryptStr);

        } catch (NoSuchAlgorithmException |
                NoSuchPaddingException |
                InvalidKeyException
                | IllegalBlockSizeException
                | BadPaddingException e) {
            e.printStackTrace();
        }
        return encrypt;
    }

    /**
     * 加密
     * @param content 明文
     * @return 密文
     */
    public static String encrypt(String content,String key){
        String encrypt = null;
        if (StringCustomUtils.isNotEmpty(content) == false) {
            return null;
        }
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES"));
            byte[] encryptStr = encryptStr = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
            encrypt = Base64.getEncoder().encodeToString(encryptStr);

        } catch (NoSuchAlgorithmException|
                NoSuchPaddingException|
                InvalidKeyException
                |IllegalBlockSizeException
                |BadPaddingException e) {
            e.printStackTrace();
        }
        return encrypt;
    }
    /**
     * 解密
     * @param content  密文
     * @return 明文
     */
    public static String decrypt(String content){
        String encrypt = null;
        if (StringCustomUtils.isNotEmpty(content) == false) {
            return null;
        }
        byte[] encryptByte = Base64.getDecoder().decode(content);
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES"));
            byte[] decryptBytes = cipher.doFinal(encryptByte);
            encrypt = new String(decryptBytes);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        return encrypt;
    }

    /**
     * 解密
     * @param content  密文
     * @return 明文
     */
    public static String decrypt(String content,String key){
        String encrypt = null;
        if (StringCustomUtils.isNotEmpty(content) == false) {
            return null;
        }
        byte[] encryptByte = Base64.getDecoder().decode(content);
        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key.getBytes(), "AES"));
            byte[] decryptBytes = cipher.doFinal(encryptByte);
            encrypt = new String(decryptBytes);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }
        return encrypt;
    }
}

